<%@ page language="java" import="com.alibaba.fastjson.JSONObject" pageEncoding="UTF-8" %>
<%@ page import="weaver.common.DateUtil" %>
<%@ page import="weaver.conn.RecordSet" %>
<%@ page import="weaver.general.BaseBean" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="weaver.hrm.HrmUserVarify" %>
<%@ page import="weaver.hrm.User" %>
<%@ page import="weaver.hrm.resource.ResourceComInfo" %>
<%@ page import="weaver.soa.workflow.request.MainTableInfo" %>
<%@ page import="weaver.soa.workflow.request.Property" %>
<%@ page import="weaver.soa.workflow.request.RequestInfo" %>
<%@ page import="weaver.soa.workflow.request.RequestService" %>
<%@ page import="weaver.workflow.request.RequestComInfo" %>
<%@ page import="weaver.workflow.workflow.WorkflowComInfo" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Map" %>
<%
    JSONObject jsonData = new JSONObject();
    String tsdate = Util.null2String( request.getParameter( "tsdate" ) );
    String resourceids = Util.null2String( request.getParameter( "resourceids" ) );
    String ids = Util.null2String( request.getParameter( "ids" ) );
    String curdate = DateUtil.getCurrentDate();
    User user = HrmUserVarify.getUser( request, response );
    int userid = user.getUID();
    RecordSet rs = new RecordSet();
    try {
        String [] arr = resourceids.split(",");
        for (String resourceid:arr) {
            String username = getDepartid(resourceid).get("lastname");
            String requestname ="培训文档学习流程-"+username+"-"+curdate;
            String workflowid = "1267";
            Map<String, String> requestMainMap = new HashMap<>(  );
            requestMainMap.put( "pxr",resourceid );
            requestMainMap.put( "pxbm",getDepartid(resourceid).get("departmentid") );
            requestMainMap.put( "cfrq",curdate );
            requestMainMap.put( "wdydjzrq",tsdate );
            String requestid = WorkflowCreateByRequestMap(userid+"",workflowid,requestname,"1",requestMainMap);
            jsonData.put( "requestid", requestid);
            rs.executeQuery("select id from formtable_main_249 where requestid=?",requestid);
            rs.next();
            String mainid = Util.null2String(rs.getString(1));
            String [] arr1 = ids.split(",");
            for (String id:arr1) {
                String wdmc = getDocidById(id);
               rs.executeUpdate("insert into formtable_main_249_dt1 (mainid,wdmc,sfyd,jmsjid) " +
                       "values ("+mainid+","+wdmc+",1,"+id+")");
            }

        }
        out.print( jsonData.toJSONString() );
    } catch (Exception e) {
        e.printStackTrace();
    }
%>
<%!
    BaseBean baseBean = new BaseBean();
    public static Map<String,String> getDepartid(String userid){
        RecordSet rs = new RecordSet();
        Map<String,String> map = new HashMap<>();
        rs.executeQuery("select lastname,departmentid from hrmresource where id=?",userid);
        rs.next();
        String lastname = Util.null2String(rs.getString("lastname"));
        String departmentid = Util.null2String(rs.getString("departmentid"));
        map.put("lastname",lastname);
        map.put("departmentid",departmentid);
        return map;
    }
    public static String getDocidById(String id){
        RecordSet rs = new RecordSet();
        rs.executeQuery("select wdmc from uf_pxwdtzsd where id=?",id);
        rs.next();
        return Util.null2String(rs.getString(1));
    }
    //创建流程方法
    public String WorkflowCreateByRequestMap(String creatorId, String workflowId, String requestName, String IsNextFlow, Map<String, String> requestMainMap) {
        baseBean.writeLog( "-----创建流程传递的参数个数--------" );
        baseBean.writeLog( "creatorId:" + creatorId );
        baseBean.writeLog( "workflowId:" + workflowId );
        baseBean.writeLog( "requestName:" + requestName );
        baseBean.writeLog( "IsNextFlow:" + IsNextFlow );

        String requestid = "";
        RequestInfo requestInfo = new RequestInfo();
        requestInfo.setCreatorid( creatorId );//创建人Id
        requestInfo.setWorkflowid( workflowId );//工作流Id
        requestInfo.setDescription( requestName );//请求标题
        if ("0".equals( IsNextFlow )) {
            requestInfo.setIsNextFlow( IsNextFlow );
        }

        //主表字段
        MainTableInfo mainTableInfo = new MainTableInfo();
        Property[] propertyArray = new Property[requestMainMap.size()];
        int p = 0;
        for (Map.Entry<String, String> entry : requestMainMap.entrySet()) {
            propertyArray[p] = new Property();
            propertyArray[p].setName( Util.null2String( entry.getKey() ) );
            propertyArray[p].setValue( Util.null2String( entry.getValue() ) );
            baseBean.writeLog( "\r\n---p=" + p + "---Key=" + entry.getKey() + "---Value=" + entry.getValue() );
            p++;
        }
        baseBean.writeLog( "-----创建流程传递的参数个数p=" + p );
        mainTableInfo.setProperty( propertyArray );
        requestInfo.setMainTableInfo( mainTableInfo );
        RequestService service = new RequestService();
        try {
            //流程
            try {
                requestid = service.createRequest( requestInfo );//创建请求id
            } catch (Exception e) {
                baseBean.writeLog( "=======" + e.getMessage() );
            }
            //String userId = requestInfo.getLastoperator();//请求最后的操作者
            //记录日志
            StringBuffer sbf = new StringBuffer( "\r\n-----xwd创建工作流记录日志开始" );
            WorkflowComInfo wfcif = new WorkflowComInfo();
            RequestComInfo rcif = new RequestComInfo();
            ResourceComInfo rscif = new ResourceComInfo();
            sbf.append( "\r\n-----姓名:" + rscif.getLastname( rcif.getRequestCreater( requestid ) ) );
            sbf.append( "\r\n-----时间:" + rcif.getRequestCreateTime( requestid ) );
            sbf.append( "\r\n-----创建流程:" + wfcif.getWorkflowname( workflowId ) );
            sbf.append( "\r\n-----请求:" + rcif.getRequestname( requestid ) );
            sbf.append( "\r\n-----请求:" + requestid );
            sbf.append( "\r\n-----创建工作流记录日志结束" );
            baseBean.writeLog( sbf.toString() );
        } catch (Exception e) {
            baseBean.writeLog( "错误：" + e );
        }
        return requestid;
    }

%>
